import { Tools,Toast } from 'weaver-mobile';
const getLabel = Tools.getLabel;

//打开流程
export const openWorkFlow = (type,requestid,returnUrl='') => {
    let url = '';
    if(type == 'add'){
        url = "/spa/workflow/forwardMobileForm.html?iscreate=1&workflowid="+requestid;
    }else if(type == 'view'){
        url = "/spa/workflow/forwardMobileForm.html?requestid="+requestid;
    }

    window.openLink.openWorkflow(url, returnUrl);
}

//获取多语言
export const getLang = () =>{
    let lang = (window.ua && window.ua.language) || 'zh_CN';
    return lang;
}

//二级路径改造
export const getEcPath = () =>{
    return (window.ecologyContentPath || "");
}

//打开三方
export const openSystemApp = (key) => {
    if (!window.em) return;

    let app = {};
    switch (key) {
        case 'travelBooking'://商旅订票
            app.iOSscheme = 'ctrip://';
            app.iOSurl = '';
            app.params = {  };
            app.androidscheme = 'ctrip://';
            app.androidurl = '';
            break;
        case 'highIronPipe'://高铁管家
            app.iOSscheme = 'gtgj://';
            app.iOSurl = '';
            app.params = { query: '' };
            app.androidscheme = 'gtgj://';
            app.androidurl = '';
            break;
        case 'didiChuxing'://滴滴打车
            app.iOSscheme = 'diditaxi://dache/sendorder';
            app.iOSurl = '';
            app.params = { };
            app.androidscheme = 'OneTravel://dache/sendorder';
            app.androidurl = '';
            break;
        case 'xiecheng'://携程
            app.iOSscheme = 'CtripWireless://';
            app.iOSurl = '';
            app.params = {};
            app.androidscheme = 'CtripWireless://';
            app.androidurl = '';
            break;
        case 'aliNailed'://钉钉
            app.iOSscheme = 'dingtalk://';
            app.iOSurl = '';
            app.params = {};
            app.androidscheme = 'dingtalk://';
            app.androidurl = '';
            break;
        case 'tcly'://同城旅游
            app.iOSscheme = 'dingtalk://';
            app.iOSurl = '';
            app.params = {};
            app.androidscheme = 'dingtalk://';
            app.androidurl = '';
            break;            

    }
    window.em.openApp(app);
}

//扫一扫
export const _scanQRCode = (callback) => {
    try {
        const isExist = window.em && window.em.checkJsApi('scanQRCode');
        if (!isExist) {Toast.fail(getLabel(525804,'对不起,暂不支持该应用'));return};
        window.em.scanQRCode({
            needResult: 1,
            scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
            success: function (res) {
                console.log('res',res);
                callback(res.resultStr);
            },
            fail:function(err){
                console.log('err',err);
                Toast.fail(err);
            }
        });
    } catch (e) {
    }
};

/**
 * 控制EM客户端顶部头信息
 */
export const controlEmTopNav = (params={},callback) => {
    const isExist = window.em && window.em.checkJsApi('configNavigation');
    if (!isExist) {Toast.fail(getLabel(525804,'对不起,暂不支持该应用'));return};
    
    const nav = {
        'success': function (argument) {
            console.log('controlEmTopNav success', JSON.stringify(argument));
        },
        'fail': function (argument) {
            console.log('controlEmTopNav fail', JSON.stringify(argument));
        },
        'left': {},
        'right': {  
            'name':'发票设置',
            'action':function(argument){
                callback();
            },
            
        },
        'title': '',
        showCloseButton: true
    };
   
    window.em.ready(function(){
        if(params.title===undefined){
            params.title = "";
        }
        window.em.configNavigation({...nav, ...params});
    });
}

/**
 * 重置导航栏
 */
export const resetNavBar = (params = {}) => {
    const isExist = window.em && window.em.checkJsApi('resetNavigation');
    if (!isExist) {Toast.fail(getLabel(525804,'对不起,暂不支持该应用'));return};

    window.em.ready(function(){
        window.em.resetNavigation({title: ''});
    });
}


/**
 * 获取 EM 主题
 */
export const getTheme = (callback) => {
    const isExist = window.em && window.em.checkJsApi('getClientInfo');
    if (!isExist) {return};

    window.em.getClientInfo({
        success:function(res){
            callback && callback(res);
        },
        fail:function(err){
            //callback && callback(err);
            //Toast.fail(err);
        }
    })
    
}

//微信发票
export const _chooseInvoice = (index,callback) => {
    try {
        const isExist = window.em && window.em.checkJsApi('chooseInvoice');
        if (!isExist) {Toast.fail(getLabel(525804,'对不起,暂不支持该应用'));return};

        window.em.chooseInvoice({
            type: index,  // 类型：1 微信  2 支付宝
            success: function (res) {
               callback && callback(res.setInvoiceList);
            },
            fail:function(err){
                callback && callback(err);
                Toast.fail(err);
            }
        });
    } catch (e) {
    }
    
}

//获取当前设备
export const getEquipment = () =>{
    let u = window.navigator.userAgent;
    let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
    let isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    let isEMobile = u.indexOf('E-Mobile') > -1;
	let isDT = u.indexOf('DingTalk') > -1;

    let reg = /MicroMessenger/i;
    let isWx = reg.test(u);

    if(!isWx){
        isWx = '';  //和微信无关
    }else{
        let isWxWork = u.match(/WxWork/i) == "wxwork";
        if(isWxWork){
            isWx = false; // 企业微信
        }else{
            isWx = true;    //微信
        }
    }

    let devNum = 0;  //0：PC    1：IOS   2：ANDROID   3：H5   4：企业微信APP   5：企业微信PC   6：微信公众号
    if(isWx === true ){
        devNum = 6;
    }else if(isWx === false){
        devNum = 4;
    }else if(isAndroid || isiOS){
        devNum = 3;
    }

    let equipment = {
        isAndroid:isAndroid,
        isiOS:isiOS,
        isEMobile:isEMobile,
        dev:devNum,
        isWx:isWx,
		isDT:isDT,
    }
    console.log('equipment',equipment);
    return equipment
}


//获取corpid
export const getCpUser = (callback) => {
    try {
        const isExist = window.em && window.em.checkJsApi('getCpUser');
        if (!isExist) {Toast.fail(getLabel(525804,'对不起,暂不支持该应用'));return};

        window.em.getCpUser({
            success: function (res) {
               callback && callback(res.cpCorpId);
            },
            fail:function(err){
                callback && callback(err);
                Toast.fail(err);
            }
        });
    } catch (e) {
    }
    
}

